/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import business.UserManager;
import entity.Event;
import entity.Participant;
import entity.Visibility;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author Alessandro This bean is intended to manage the load and visualization
 * of user's calendar
 *
 */
@ManagedBean
@ViewScoped
public class CalendarBean implements Serializable {

    private ScheduleModel eventModel;
    private ScheduleEvent selectedEvent = new DefaultScheduleEvent();

    private List<Event> calendar;

    @EJB
    private UserManager um;

    @PostConstruct
    /**
     * load the list of all user's event's in this.calendar
     */
    public void init() {
        List<Event> cal2 = um.loadCalendar();
        calendar = new ArrayList();
        eventModel = new DefaultScheduleModel();
        for (Event e : cal2) {
            //if i partake the event i push into the scheduler
            if ((isPartecipating(e))) {
                DefaultScheduleEvent schedEv = new DefaultScheduleEvent(e.getTitle(), e.getStartDate(), e.getEndDate(), e);
                if (um.getLoggedUser().equals(e.getCreator())) {
                    schedEv.setStyleClass("eventCreator");
                }else{
                    schedEv.setStyleClass("eventParticipant");
                }
                eventModel.addEvent(schedEv);
            }
            //if the date is not already passed i push into the list of myEvents
            if (!(e.getEndDate().before(Calendar.getInstance().getTime()))){
                calendar.add(e);
            }
        }
    }

    public List<Event> getCalendar() {
        return calendar;
    }

    public void setCalendar(List<Event> calendar) {
        this.calendar = calendar;
    }

    public ScheduleModel getEventModel() {
        return eventModel;
    }

    public void setEventModel(ScheduleModel eventModel) {
        this.eventModel = eventModel;
    }

    public ScheduleEvent getSelectedEvent() {
        return selectedEvent;
    }

    public void setSelectedEvent(ScheduleEvent selectedEvent) {
        this.selectedEvent = selectedEvent;
    }

    public void onEventSelect(SelectEvent selectEvent) {
        selectedEvent = (ScheduleEvent) selectEvent.getObject();
    }

    public String redirectToEvent() {
        Event e = (Event) selectedEvent.getData();
        return "eventPage?faces-redirect=true&idEv=" + e.getId();
    }

    private boolean isPartecipating(Event e) {
        for (Participant p : e.getParticipantCollection()) {
            if (p.getUser1().equals(um.getLoggedUser())) {
                if (p.getAnswer().equals("yes")) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }
    
    public String getTimeZone(){
        return Calendar.getInstance().getTimeZone().getID();
    }
    
    public String color(Event e) {
        if (e.getCreator().equals(um.getLoggedUser())) {
            return "#ff8500";
        } else {
            return "#b2ddff" ;
        }
    }
}
